<!--
 * @Description: 招聘全局设置
 * @Author: liyujie
 * @Date: 2021/09/22
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/09/22
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <page-header title="全局设置">
                <template slot="header-right">
                    <div
                        class="back"
                        @click="handleRoute('recruitCompanyList')"
                    >返回</div>
                </template>
            </page-header>
            <div>
                <div class="form-view">
                    <!--表单-->
                    <el-form
                        :model="ruleForm"
                        :rules="rules"
                        label-position="right"
                        ref="ruleForm"
                        label-width="192px"
                        class="form-wrap"
                        @submit.native.prevent
                    >
                        <el-form-item label="招聘协议">
                            <ueditor
                                :default-value="ruleForm.apply_work_recruit_rule_content"
                                ref="ueditor"
                                style="width: 862px"
                            />
                        </el-form-item>
                        <el-form-item>
                            <div
                                class="form-tips"
                                style="width: 862px"
                            >
                                <span>注意：在输入招聘协议的时候请仔细编写协议的段落样式，此处设置的段落样式、文案样式将会在商家发布招聘的时候显示；</span>
                                <el-button
                                    size="medium"
                                    @click="handlePreview"
                                >
                                    预览协议
                                </el-button>
                            </div>
                        </el-form-item>
                    </el-form>
                </div>

                <!-- button -->
                <div class="footer">
                    <el-button
                        size="medium"
                        type="default"
                        @click="handleCancel()"
                    >
                        返回
                    </el-button>
                    <el-button
                        size="medium"
                        type="primary"
                        @click="handleSubmit()"
                    >
                        保存
                    </el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import ueditor from '@/components/common/ueditor.vue';
import {deepClone} from "@/assets/js/utils";

export default {
    name: 'RecruitConfig',
    data() {
        return {
            ruleForm: {
                // 协议
                apply_work_recruit_rule_content: '',
            },
            rules: {
                apply_work_recruit_rule_content: [
                    {required: true, message: '请填写招聘协议', trigger: 'blur'},
                ],
            },
            authResult: {}
        };
    },
    mixins: [back],
    watch: {},
    components: {ueditor},
    mounted() {
        this.init();
    },
    filters: {},
    methods: {
        /**
         * @description 页面初始化
         */
        init() {
            this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '人力资源'}, {title: '招聘企业列表', path: '/student/recruit-company-list'}, {title: '全局设置'}]);
            this.authResult = this.$route.meta.authResult;
            this.getConfig();
        },

        /**
         * 获取
         */
        getConfig() {
            this.$post('/student/work_recruit%5Cget_config', {}, resp => {
                if (resp.code === 1) {
                    this.$nextTick(() => {
                        this.$refs.ueditor.setContent(resp.data.apply_work_recruit_rule_content);
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 提交
         */
        handleSubmit() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.ruleForm.apply_work_recruit_rule_content = this.$refs.ueditor ? this.$refs.ueditor.getUEContent() : '';
                    let params = deepClone(this.ruleForm);

                    if(!params.apply_work_recruit_rule_content) {
                        this.$notify({
                            title: '提示',
                            message: '调用协议不能为空',
                            type: 'warning'
                        });
                        return;
                    }
                    this.$post('/student/work_recruit%5Cset_config', params, resp => {
                        if (resp.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '保存成功',
                                duration: 2000,
                                type: 'success'
                            });
                            this.$router.push({
                                name: 'recruitCompanyList'
                            });
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                duration: 2000,
                                type: 'warning'
                            });
                        }
                    });
                } else {
                    // 校验不通过
                    if(document.getElementsByClassName('el-form-item__error').length > 0){
                        this.$notify.warning({
                            title: '提示',
                            message: document.getElementsByClassName('el-form-item__error')[0].innerText
                        });
                    }
                    return false;
                }
            });
        },

        /**
         * @description 返回
         */
        handleCancel() {
            if (this.type === 'detail') {
                this.$router.push({
                    name: 'recruitCompanyList'
                });
            } else {
                this.$zdConfirm('离开此页面后，已填写的信息将不会保存，确认离开此页面吗？', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$router.push({
                        name: 'recruitCompanyList'
                    });
                }).catch(() => {});
            }
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * 预览
         */
        handlePreview() {
            let content = this.$refs.ueditor ? this.$refs.ueditor.getUEContent() : '';

            localStorage.setItem('ACTIVITY_AGREEMENT', content);
            const routeData = this.$router.resolve({
                name: 'activityAgreementPreview',
            });

            window.open(routeData.href, 'activityAgreementPreview');
        },
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;
        overflow-y: auto;
        &::-webkit-scrollbar {
            width: 4px;
        }
        .back {
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
            cursor: pointer;
        }

        /deep/ .el-form {
            .is-required {
                .el-form-item__label {
                    &:before {
                        content: "*";
                        display: inline-block;
                        color: #F56C6C;
                    }
                }
            }
            .image-label {
                .el-form-item__label {
                    margin-top: 40px;
                }
            }
        }

        .form-view {
            background: #FBFBFB;
            border-radius: 2px;
            border: 1px solid #EEEEEE;
            margin: 24px 32px;
            box-sizing: border-box;
            padding: 32px;

            .form-tips {
                font-size: 12px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #999999;
                display: flex;
                align-items: center;
                justify-content: space-between;
            }

            .ueditor-tips {
                width: 862px;
                font-size: 12px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #FF8E39;
                display: flex;
                align-items: center;
                justify-content: space-between;
            }

            .form-title {
                font-size: 16px;
                font-family: PingFangSC-Semibold, PingFang SC;
                font-weight: 600;
                color: #333333;
                padding-bottom: 12px;
                border-bottom: 1px solid #E9E9E9;
                margin-bottom: 24px;
                margin-top: 60px;
            }
        }

        /*footer*/

        .footer {
            box-sizing: border-box;
            padding-left: 176px;
            margin-top: 8px;
            margin-bottom: 52px;

            .el-button {
                width: 120px;
                margin: 0 16px 12px;
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0px 1px 0px 0px rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }

    /deep/.edui-default {
        .edui-editor {
            width: 862px!important;
        }
    }
    /deep/ .el-input-number .el-input__inner {
        text-align: left;
    }

    /deep/ .pic-view {
        display: flex;
        align-items: flex-end;

        .tips {
            margin-bottom: 40px;
        }
        .pic-item {
            margin-bottom: 0!important;
        }
        .uploader-customer {
            background: #fff!important;
        }
    }
}

/deep/ .el-table {
    &::before {
        height: 0;
    }
    .el-table__row {
        &:last-of-type {
            td {
                border: none;
            }
        }
    }
    th {
        background: #f2f2f2!important;
        font-size: 14px!important;
        font-family: PingFangSC-Medium, PingFang SC!important;
        font-weight: 500!important;
        color: #333333!important;
        height: 44px!important;
        padding: 0!important;
    }
}
</style>

